package com.mxprojects.constructors
{
	import com.mxprojects.gui.Styles;
	import com.mxprojects.io.Config;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	public class ButtonConstructor extends MovieClip
	{		
				
		private var eventListener:Function;
		private var label:TextField;
		private var background:MovieClip;
		
		public function ButtonConstructor(){}
		
		protected function setupButton(text:String, clickFunction:Function, label:TextField, background:MovieClip):void {
			//set the listener
			this.eventListener = clickFunction;
			
			//set the label and BG
			this.label = label;
			this.background = background;
			this.background.gotoAndStop(1);
			
			//throw the text in
			this.label.autoSize = TextFieldAutoSize.LEFT;
			this.label.x = Config.BUTTON_PADDING;
			this.label.mouseEnabled = false;
			this.label.styleSheet = Styles.css;
			this.label.htmlText = text;
			this.label.antiAliasType = AntiAliasType.ADVANCED;
						
			//set the bg
			this.background.width = this.label.width + Config.BUTTON_PADDING*2;
						
			//set the event listeners
			enabled = true;
		}
		
		override public function set enabled(isEnabled:Boolean):void {
			if(isEnabled){
				this.background.alpha = 1;
				this.addEventListener(MouseEvent.CLICK, eventListener, false, 0, true);
				this.addEventListener(MouseEvent.ROLL_OVER, rollOver, false, 0, true);
				this.addEventListener(MouseEvent.ROLL_OUT, rollOut, false, 0, true);
			}else{
				this.background.alpha = Config.BUTTON_DISABLED_ALPHA;
				this.removeEventListener(MouseEvent.CLICK, eventListener);
				this.removeEventListener(MouseEvent.ROLL_OVER, rollOver);
				this.removeEventListener(MouseEvent.ROLL_OUT, rollOut);
			}
			
			//force it to the first frame
			this.background.gotoAndStop(1);
		}
		
		public function changeLabel(text:String):void {
			//pretty simple, change the label of a button
			this.label.htmlText = text;
			this.background.width = this.label.width + Config.BUTTON_PADDING*2;
		}
		
		private function rollOver(event:MouseEvent):void {
			this.background.gotoAndPlay("over");
		}
		
		private function rollOut(event:MouseEvent):void {
			this.background.gotoAndPlay("out");
		}

	}
}